<?php
define('MINSIZE' , 100);

function get_client_ip() {
	if (isset($_SERVER['HTTP_CLIENT_IP']) and !empty($_SERVER['HTTP_CLIENT_IP']))
		return $_SERVER['HTTP_CLIENT_IP'];
	if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and !empty($_SERVER['HTTP_X_FORWARDED_FOR']))
		return strtok($_SERVER['HTTP_X_FORWARDED_FOR'], ',');
	if (isset($_SERVER['HTTP_PROXY_USER']) and !empty($_SERVER['HTTP_PROXY_USER']))
		return $_SERVER['HTTP_PROXY_USER'];
	if (isset($_SERVER['REMOTE_ADDR']) and !empty($_SERVER['REMOTE_ADDR']))
		return $_SERVER['REMOTE_ADDR'];
	return "0.0.0.0";
}


/**
* Create a new directory, and the whole path.
*
* If the parent directory does not exists, we will create it, etc.
*
* @param string the directory to create
* @param int the mode to apply on the directory
* @return bool return true on success, false else
*/
function mkdirs($dir, $mode = 0777) {
	if( is_dir($dir) )
		return true;

	$stack = array(basename($dir));
	$path = null;
	while ( ($d = dirname($dir) ) ) {
		if ( !is_dir($d) ) {
			$stack[] = basename($d);
			$dir = $d;
		} else {
			$path = $d;
			break;
		}
	}

	if ( ( $path = realpath($path) ) === false )
		return false;

	$created = array();
	for( $n = count($stack) - 1; $n >= 0; $n-- ) {
		$s = $path . '/'. $stack[$n];
		if ( !mkdir($s, $mode) ) {
			for ( $m = count($created) - 1; $m >= 0; $m-- )
			rmdir($created[$m]);
			return false;
		}
		chmod($s , $mode);
		$created[] = $s;
		$path = $s;
	}
	return true;
}

/**
 * Write a file. Create directory auto.
 *
 * @param string $file_name
 * @param string $dir
 * @param string $data
 * @param boolean $if_mkdir
 * @param string $method
 * @param boolean $if_return
 * @return string | boolean
 */
function writetofile($file_name , $dir = '.' , $data = '' , $if_mkdir = 0 , $method = "w" , $if_return = 0) {
	if($if_mkdir == 1 && !is_dir($dir)) {
		mkdirs($dir);
	}
	$filenum = fopen($dir . '/' .$file_name , $method);
	flock($filenum,LOCK_EX);
	$file_data = fwrite($filenum,$data);
	fclose($filenum);

	if($if_return == 1) {
		return $file_data;
	} else {
		return true;
	}
}

/**
 * 把url解析为xml
 *
 * @param string $url
 * @return unknown
 */
function parse_xml_to_array($url){
	$data = @windows_get_file($url);
	if($data)
	{
		$xml = new XMLparser($data);
		return $xml->data[0];
	}
	return false;
}

/**
 * 获取url，并存储在本地
 *
 * @param string $url
 * @param string $save_file
 * @return boolean
 */
function windows_get_file($url , $cookie = false) {
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url);
	
	if($cookie) {
		curl_setopt($ch , CURLOPT_COOKIEJAR , $cookie);
		curl_setopt($ch , CURLOPT_COOKIEFILE , $cookie);
	}
	
	curl_setopt($ch , CURLOPT_REFERER , $url);
	
	//设定是否输出页面内容
	curl_setopt($ch, CURLOPT_NOBODY, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
	curl_setopt($ch, CURLOPT_USERAGENT, 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQPinyin 689; QQDownload 615; TencentTraveler 4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)');
	
	$data = curl_exec($ch);

	curl_close($ch);
	
	return $data;
	
	
	
	$data = file_get_contents($url);
	$file_size = strlen($data);
	if($file_size < MINSIZE){
		echo "<b style='color:red;'>Error(3): </b>File is too small({$file_size})..<br />\n\n";
		return false;
	}
	/*
	if($file_size > MAXSIZE){
		echo "<b style='color:red;'>Error(4): </b>File is too big({$file_size})..<br />\n\n";
		return false;
	}
	*/
	if(file_put_contents($save_file , $data)){
		return $data;
	}
	return false;
}

/**
 * 自动上传
 *
 * @param string $upload_url
 * @param array $form_data
 * @param unknown_type $file_path
 * @param unknown_type $file_form_name
 * @param unknown_type $file_name
 * @return unknown
 */
function autoupload($upload_url , $form_data , $file_path , $file_form_name , $file_name){
	$pf = new IBPostFile( $upload_url );
	$pf->setForm($form_data);

	if($file_path) {
		$pf->setFile( $file_form_name , $file_path , $file_name );
	}
	$result = $pf->sendRequest();
	if($result === false) return "FsocketOpen Failed.";
	return $pf->getBody();
}

# end of script
